package com.apes.fn.business.carAccident.modle;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.crm.business.model.InsuranceCompany;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

/**
 * @ClassName ClaimWorkOrder
 * @Author wsj
 * @Date 2021/6/4 10:38
 * @description 定损工单
 */


@Entity
@EntityListeners(DefaultPersistListener.class)
@Getter
@Setter
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
@Table(name = ClaimWorkOrder.CONST_TABLE_NAME)
public class ClaimWorkOrder extends Model {
    public final static String CONST_TABLE_NAME = "claim_work_order";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "定损工单", padding = 5, prefix = "DG%(yy)%(month)", useDateRange = true)
    @Comment("定损工单号")
    private String id;

    @Column(name = "business_type", length = 32)
    @Comment("单据类型")
    private String businessType = "claimWorkOrder";

    @Column(name = "business_type_name", length = 64)
    @Comment("单据类型名称")
    private String businessTypeName = "定损工单";

    @Column(name = "source_from", length = 16)
    @Comment("来源单号")
    private String sourceFrom;

    @Column(name = "source_From_Name", length = 64)
    @Comment("单据来源")
    private String sourceFromName;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "新增"),
            @State(id = "accept", name = "接受代勘"),
            @State(id = "finish", name = "完成代勘"),
            @State(id = "goBack", name = "退回代勘"),
            @State(id = "repeal", name = "终止"),
    })
    @Column(name = "state", length = 32, nullable = false)
    @Comment("单据状态")
    private String state = "create";

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("门店")
    private Dept dept;
    /*
        A:代勘到店, B:......
     */

    @Column(name = "work_Order_Type", length = 16)
    @Comment("定损工单类型")
    private String workOrderType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "insurance_company_id", referencedColumnName = "id")
    @Comment("保险公司")
    private InsuranceCompany insuranceCompany;

    @Column(name = "car_license", length = 16)
    @Comment("车牌")
    private String carLicense;

    @Column(name = "report_number", length = 64)
    @Comment("报案号")
    private String reportNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_Staff", referencedColumnName = "id")
    @Comment("商务员工")
    private PartyRole businessStaff;

    @Column(name = "business_Staff_Name", length = 64)
    @Comment("商务人名")
    private String businessStaffName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_Staff", referencedColumnName = "id")
    @Comment("门店人员")
    private PartyRole storeStaff;

    @Column(name = "store_Staff_Name", length = 64)
    @Comment("门店人名")
    private String storeStaffName;

    @Column(name = "report_person", length = 16)
    @Comment("报案人")
    private String reportPerson;

    @Column(name = "phone_number", length = 16)
    @Comment("报案人电话号")
    private String phoneNumber;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "car_accident_date", length = 32)
    @Comment("出险日期")
    private Date carAccidentDate;

    @Column(name = "car_accident_place", length = 64)
    @Comment("出险地点")
    private String carAccidentPlace;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "finish_uid", referencedColumnName = "id")
    @Comment("完成人")
    private PartyRole finishUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "finish_date")
    @Comment("完成日期")
    private Date finishDate;


    @OneToMany(mappedBy = "claimWorkOrder", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("id desc")
    private List<WorkOrderOperationRecord> operationRecords = new ArrayList<>();

    public List<WorkOrderOperationRecord> getOperationRecords() {
        if (operationRecords == null ){
            return new ArrayList<>();
        }
        for (WorkOrderOperationRecord item : operationRecords) {
            item.setClaimWorkOrder(this);
        }
        return operationRecords;
    }

    @Transient
    public static  final Map<String,String > FIELD_COMMENTARY = new HashMap<String, String>() {
        {
            put("A", "代勘到店");
            put("B", "其他");
        }
    };

    @Transient
    private String orderType;

    public String getOrderType() {
        if (workOrderType == null) {
            return "";
        }else {
            return FIELD_COMMENTARY.get(workOrderType);
        }
    }
}
